package demo1;

public class LeetCode904 {
    public int totalFruit(int[] fruits) {
        int n = fruits.length, kinds = 0, ret = 0;
        int[] hash = new int[n];
        for(int left = 0, right = 0; right < n; right++) {
            int in = fruits[right];
            if(hash[in] == 0) kinds++;
            hash[in]++;
            while(kinds > 2) {
                int out = fruits[left];
                hash[out]--;
                if(hash[out] == 0) kinds--;
                left++;
            }
            ret = Math.max(ret, right - left + 1);
        }
        return ret;
    }
}
